package com.home.wechat_.ui

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.home.wechat_.ext.percentOffsetX

/**
 *Author: YFS(893145181@qq.com)
 *Time:2021/12/2 9:24
 */
@Composable
fun Home() {
    val mViewModel: WeViewModel = viewModel()
    Column() {
        val pagerState: PagerState = run {
            remember(mViewModel.theme) {
                PagerState(maxPage = 3)
            }
        }

        Pager(state = pagerState, Modifier.weight(1f)) {
            when (page) {
                0 -> {
                    ChatList(mViewModel.chats)
                }

                1 -> {
                    Text(text = "111111111", Modifier.fillMaxSize())
                }

                2 -> {
                    Text(text = "222222222", Modifier.fillMaxSize())
                }

                3 -> {
                    Text(text = "3333333333", Modifier.fillMaxSize())
                }
            }
        }
        WeBottomBar(mViewModel.selectedTab) {
            pagerState.currentPage = it
        }
    }

    val openOffSet by animateFloatAsState(targetValue = if (mViewModel.openModule == null) 1f else 0f)
    if (mViewModel.currentChat != null) {
        ChatPage(
            Modifier.percentOffsetX(openOffSet),
            chat = mViewModel.currentChat
        ) {
            mViewModel.endChat()
        }
    }
}